package ug.co.icom.kyazze.healthticket;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import ug.co.icom.kyazze.healthticket.data.User;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;

public class ProfileFragment extends Fragment implements OnDateSetListener{

	//private User user;
	private TextView phoneNumber;
	private EditText fName;
	private EditText lName;
	private EditText oNames;
	private TextView dOB;
	private Spinner bloodType;
	private ToggleButton sex;
	private EditText email;
	private EditText address;
	private ImageView imgProfile;
	private Bitmap bitmap;
	private String filepath = "healthTicket_files";
	
	String uName;	
	String pWord;
	String fNamep;
	String lNamep;
	String oName;
	String add;
	String bt;
	Boolean fem;
	String dob;
	String ema;

	Locale locale = Locale.getDefault();
	private int year;
	private int month;
	private int day;

	public ProfileFragment()
	{

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		

		SharedPreferences settings = this.getActivity().getSharedPreferences("ULS", Context.MODE_PRIVATE);
		
		uName  = settings.getString("uName","");	
		pWord  = settings.getString("pWord","");
		fNamep  = settings.getString("fName","");
		lNamep  = settings.getString("lName","");
		oName  = settings.getString("oName","");
		add  = settings.getString("add","");
		bt  = settings.getString("bt","");
		fem  = settings.getBoolean("fem", false);
		dob  = settings.getString("dob","");
		ema  = settings.getString("ema","");
		
		//user = (User)getArguments().getSerializable("user");

		View rootView = inflater.inflate(R.layout.profile_layout, container, false);
		phoneNumber = (TextView) rootView.findViewById(R.id.textView3);
		phoneNumber.setText(uName);
		
		
		ContextWrapper contextWrapper = new ContextWrapper(getActivity().getApplicationContext());
		File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
		File file3= new File(directory+"/"+"kyazze.jpg");
		
		if(file3.exists()){
			
			//final Bitmap b = BitmapFactory.decodeFile(directory+"/"+"0.jpeg");

		    Bitmap myBitmap = BitmapFactory.decodeFile(file3.getAbsolutePath());
		    
		    imgProfile = (ImageView) rootView.findViewById(R.id.imgProfile);  
		    imgProfile.setImageBitmap(myBitmap);

		}

		//bitmap = file3;
		

		fName = (EditText) rootView.findViewById(R.id.editText1);
		fName.setText(fNamep);

		lName = (EditText) rootView.findViewById(R.id.editText2);
		lName.setText(lNamep);

		oNames = (EditText) rootView.findViewById(R.id.editText3);
		oNames.setText(oName);

		dOB = (TextView) rootView.findViewById(R.id.textView12);
		//dOB.setText(getStringFromDate(user.DateOfBirth));

		bloodType = (Spinner) rootView.findViewById(R.id.spinner1);
		bloodType.setSelection(getItemPosition(bt));

//		sex = (ToggleButton) rootView.findViewById(R.id.toggleButton1);
//
//		sex.setChecked(user.Female);

		email = (EditText) rootView.findViewById(R.id.editText5);
		email.setText(ema);

		address = (EditText) rootView.findViewById(R.id.editText6);
		address.setText(add);
		return rootView;
	}

	private int getItemPosition(String bloodType) {
		if(bloodType.equals("A+"))
		{
			return 0;
		}
		else if(bloodType.equals("B+"))
		{
			return 1;
		}
		else if(bloodType.equals("AB+"))
		{
			return 2;
		}
		else if(bloodType.equals("O+"))
		{
			return 3;
		}
		else if(bloodType.equals("A-"))
		{
			return 4;
		}
		else if(bloodType.equals("B-"))
		{
			return 5;
		}
		else if(bloodType.equals("AB-"))
		{
			return 6;
		}
		else if(bloodType.equals("O-"))
		{
			return 7;
		}
		else 
		{
			return 8;
		}
	}

	private String getStringFromDate(Date dateReceived) {
		SimpleDateFormat  dateFormat = new SimpleDateFormat("dd-MM-yyyy", locale);  
		try {  
			String date = dateFormat.format(dateReceived);  
			return date;  
		} catch (Exception e) {  
			return "";
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		try {

			dOB.setOnClickListener(new View.OnClickListener() {                       
				@Override
				public void onClick(View v) {
					MyDateDialog dialog = new MyDateDialog();
					FragmentManager fragmentManager = getFragmentManager();
					dialog.setTargetFragment(ProfileFragment.this, 0);
					dialog.show(fragmentManager, null);
				}

			});

		} catch (ClassCastException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onDateSet(DatePicker view, int selectedYear, int selectedMonth,
			int selectedDay) {
		year = selectedYear;
		month = selectedMonth;
		day = selectedDay;

		// set selected date into textview
		dOB.setText(new StringBuilder().append(day)
				.append("-").append(month + 1).append("-").append(year));

	}
}
